package learnJava.puzzlers;

public class IntPuzzlers {

    public static class IsOdd {
        // 负数%正数等于0或负数
        public static boolean isOddWrong(int val) {
            return val % 2 == 1;
        }

        public static boolean isOdd(int val) {
            return val % 2 != 0;
        }

        public static void main(String[] args) {
            assert IsOdd.isOdd(-11) : "wrong";
            assert IsOdd.isOddWrong(-11) : "wrong";
        }
    }

    public static class LongDivision {
        public static void main(String[] args) {
            // 24 * 60 * 60 * 1000 * 1000 是int型变量，这里溢出了
            final long MICRO_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
            final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
            System.out.println(MICRO_PER_DAY / MILLIS_PER_DAY);
            //修改方案24L是long这样就不会溢出了
            final long MICRO_PER_DAY1 = 24L * 60 * 60 * 1000 * 1000;
            final long MILLIS_PER_DAY1 = 24L * 60 * 60 * 1000;
            System.out.println(MICRO_PER_DAY1 / MILLIS_PER_DAY1);

        }
    }

    public static class JoyofHex{
        public static void main(String[] args) {
            System.out.println(Long.toHexString(0x100000000L+0xcafebabe));
            //0xcafebabe为负数扩宽保留符号0xffffffffcafebabe
            //== 0x100000000+0xffffffffcafebabe
            //修改为
            System.out.println(Long.toHexString(0x100000000L+0xcafebabeL));
        }
    }

    public static class Multicast{
        public static void main(String[] args) {
            System.out.println((int)(char)(byte) -1);
        }
        //byte to char ：扩宽为int再缩窄为char
        // -1 -> 0xff -> -1 -> 0xffff -> 0x0000ffff
    }

    public static class DosEquis{
        //混合条件表达式应该使用相同的类型
        public static void main(String[] args) {
            char x = 'X';
            int i = 0;
            System.out.print(true ? x : 0);
            System.out.print(false ? i : x);
        }
    }

    public static class BigDelight{
        public static void main(String[] args) {
            for (byte b=Byte.MIN_VALUE;b<Byte.MAX_VALUE;b++){
                if(b==0x90) // b会被拓宽为int型而且它的值永远不会大于127
                    System.out.println("Joy!");
            }
        }
    }

}
